package com.jbh.netlibrary.m;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import com.jbh.netlibrary.i.Callback;
import com.jbh.netlibrary.u.DeviceNetUtil;
import com.jbh.netlibrary.u.ForActivityUtil;
import com.jbh.netlibrary.u.InitConfig;
import com.jbh.netlibrary.v.NetErrorView;

public class NetManager {

    private Activity context;
    private static NetManager manager;

    private NetManager() {
    }


    public static NetManager getInstance() {
        if (manager == null) {
            synchronized (NetManager.class) {
                if (manager == null) {
                    manager = new NetManager();
                }
            }
        }
        return manager;
    }

    public InitConfig config;
    public NetManager setConfig(InitConfig config) {
        if (this.config == null) {
            this.config = config;
        }
        return this;
    }


    public void setIcon() {
        if (NetErrorView.errorView != null && config.getErrorIcon() != 0) {
            NetErrorView.errorView.setIm_error(config.getErrorIcon());
        }
    }



    /**
     * 刷新
     */
    public void refresh(Context context) {
        DeviceNetUtil.checkConnected(context, new Callback<Integer>() {
            @Override
            public void onState(Integer integer) {
                if (integer == 0){
                    NetErrorView.dismiss();
                }
            }
        });
    }


    public void onResume(Activity activity) {
        this.context = activity;
        DeviceNetUtil.registerReceiver(activity, connectivityChangeReceiver);
    }


    public void onPause() {
        DeviceNetUtil.unregisterReceiver(context, connectivityChangeReceiver);
    }


    public void onDestroy() {
        NetErrorView.onDestroy();
    }

    public void show(final Context context) {
        new AlertDialog.Builder(context)
                .setTitle("温馨提示")
                .setMessage("网络连接错误，请确保手机网络正常连接！")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        toSettingActivity(context);
                    }
                }).show();
    }


    /**
     * 去设置页面
     *
     * @param context
     */
    public void toSettingActivity(Context context) {
        Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
        context.startActivity(wifiSettingsIntent);
    }


    private final DeviceNetUtil.ConnectivityChangeReceiver connectivityChangeReceiver = new DeviceNetUtil.ConnectivityChangeReceiver() {

        @Override
        protected void onDisconnected() {
            if (context != null) {
                NetErrorView.show(ForActivityUtil.getAppCompatActivity(context));
                setIcon();
            }
            TOAST(context, "网络已断开，请检查！");
            Log.v("net_api", "[net] - > " + "onDisconnected");
        }

        @Override
        protected void onConnected() {
            NetErrorView.dismiss();
            Log.v("net_api", "[net] - > " + "onConnected");
        }
    };



    /**
     * @param activity
     * @param msg
     */
    public static void TOAST(Context activity, String msg) {
        Toast toast = new Toast(activity);
        LinearLayout layout = new LinearLayout(activity);
        layout.setPadding(0,0,0,60);
        TextView textView = new TextView(activity);
        layout.addView(textView);
        textView.setText(msg);
        textView.setTextColor(Color.WHITE);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(Color.parseColor("#81000000"));
        drawable.setCornerRadius(10);
        textView.setBackground(drawable);
        textView.setPadding(20, 20, 20, 20);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(2000);
        toast.setView(layout);
        toast.show();
    }
}
